{include file="public/header" /}



<div class="page">

    <form id="answer_form" method="post" name="articleClassForm">
        <table class="ds-default-table">
            <tbody>
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation" for="ac_name">选择已有答案:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform">
                    <select name="" id="answer-select" >
                        <option value="0">请选择</option>
                        {notempty name="answer_list"}
                        {foreach $answer_list as $key => $val}
                        <option class="del-otpion-{$val->id}" value="{$val->id}">{$val->content}</option>
                        {/foreach}
                        {/notempty}
                    </select>
                    <button type="button" id="add-select" data-content="" data-id="" class="btn btn-danger btn-small">添加</button>
                </td>
            </tr>
            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation" for="ac_name">内容:</label>
                </td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform">
                    <input type="text" name="content" id="content" class="txt">
                    <input type="hidden" name="qeustion_id" id="qeustion_id" value="{$question_id}">
                </td>
            </tr>

            <tr class="noborder">
                <td colspan="2" class="required">
                    <label class="validation" for="ac_name">关联答案:</label>
                </td>
            </tr>
            {notempty name="answer"}
            {foreach $answer as $key => $val}
            <tr class="noborder answer_{$val->id}">
                <td class="vatop rowform">
                     {$val->content} &nbsp; <button type="button" class="btn btn-danger btn-small delete" attr-data="{$val->content}" data-id="{$val->id}">删除</button>
                </td>
            </tr>
            {/foreach}
            {/notempty}
            </tbody>
            <tfoot>
            <tr class="tfoot">
                <td colspan="15" >
                    <input class="btn add-answer" type="button" value="{$Think.lang.ds_submit}"/>
                </td>
            </tr>
            </tfoot>
        </table>
    </form>
</div>
<script type="text/javascript" src="{$Think.ADMIN_SITE_ROOT}/js/jquery.edit.js" charset="utf-8"></script>
<script type="text/javascript">
    $('.add-answer').click(function () {
      var content = $('#content').val();
      var question_id = $('#qeustion_id').val();
      $.post('addAnswer.html?question_id='+question_id, {
          'content': content
      }, function (res) {
            res = JSON.parse( res );
            console.log(res)
            if (res.code == '10000') {
                layer.msg(res.message)
                var html = '<tr class="noborder answer_'+res.id+'">\n' +
                    '                <td class="vatop rowform">\n' +
                    '                     '+content+' &nbsp; <button type="button" class="btn btn-danger btn-small delete" attr-data="'+content+'" data-id="'+res.id+'">删除</button>\n' +
                    '                </td>\n' +
                    '            </tr>';
                $('tbody').append(html)
                $('#content').val('');
            }

      }, 'json')
    })
    $('.delete').click(function () {
        var answer_id = $(this).attr('data-id');
        var question_id = $('#qeustion_id').val();
        layer.confirm('确定要解除"'+$(this).attr('attr-data')+'"关联答案吗', {
            btn: ['确定', '取消'],
            title: false,
        }, function () {
            $.get('deleteAnswer?question_id='+question_id+'&answer_id='+answer_id, function (res) {
                res = JSON.parse( res );
                layer.msg(res.message);
                if (res.code == 10000) {
                    $('.answer_'+answer_id).remove();
                }
            })
        });
    })
    $('#add-select').click(function () {
        var answer_id = $(this).attr('data-id');
        if (!answer_id) {
            layer.msg('请选择答案');
            return false;
        }
        var question_id = $('#qeustion_id').val();
        $.get('AddAnswerSelect?question_id='+question_id+'&answer_id='+answer_id, function (res) {
            res = JSON.parse( res );
            layer.msg(res.message);
            if (res.code == 10000) {
                $('.del-otpion-'+answer_id).remove();
                var html = '<tr class="noborder answer_'+$('#add-select').attr('data-id')+'">\n' +
                    '                <td class="vatop rowform">\n' +
                    '                     '+$('#add-select').attr('data-content')+' &nbsp; <button type="button" class="btn btn-danger btn-small delete" attr-data="'+$('#add-select').attr('data-content')+'" data-id="'+$('#add-select').attr('data-id')+'">删除</button>\n' +
                    '                </td>\n' +
                    '            </tr>';
                $('tbody').append(html)
            }
        })


    })
    $('#answer-select').change(function () {
        $('#add-select').attr('data-id', $(this).val())
        var content = $('.del-otpion-'+$(this).val()).text();
        $('#add-select').attr('data-content', content)
    })
</script>
